<template>
  <div class="app-container">
    <div class="filter-container">
      品牌名称:
      <el-input v-model="listQuery.name" label="品牌名称" placeholder="关键词" style="width: 200px;"
        class="filter-item mring10" @keyup.enter.native="handleFilter" suffix-icon="el-icon-search" />
      <el-button v-waves class="filter-item mring10" type="primary" icon="el-icon-search" @click="handleFilter"
        size="small"> 搜索</el-button>
      <el-button v-waves type="success" icon="el-icon-plus" @click="handleCreate()" size="small"> 添加品牌</el-button>
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" fit stripe highlight-current-row
      style="width: 100%;margin-top: 1.125rem" @sort-change="sortChange" row-key="id">
      <!-- <el-table-column label="编号" prop="id" sortable="custom" align="center" width="80" :class-name="getSortClass('id')"></el-table-column> -->
      <el-table-column label="名字" prop="name" min-width="150"></el-table-column>
      <el-table-column label="图标" prop="ico_img" min-width="120">
        <template slot-scope="{row}">
          <img @click="showImageCenter" v-if="row.ico_img" :src="row.ico_img" class="avatar">
        </template>

      </el-table-column>
      <el-table-column label="排序" prop="sort" min-width="120"></el-table-column>
      <el-table-column label="状态" prop="status_name" class-name="status-col" width="100"></el-table-column>
      <el-table-column label="创建时间" prop="create_time" min-width="150" align="center">
        <template slot-scope="{row}">
          {{ row.create_time | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" @click="handleUpdate(row)" size="small">编 辑</el-button>
          <el-button type="danger" @click="handleDelete(row, $index)" size="small">删 除</el-button>
        </template>
      </el-table-column>
      <!-- <template slot="empty" >
        <span style="font-size: 20px">会员可以点击'选择产品'进行数据查看</span>
      </template> -->
    </el-table>
    <pagination hide-on-single-page v-show="total > 0" :total="total" :page.sync="listQuery.page"
      :limit.sync="listQuery.size" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model.sync="temp" label-position="left" label-width="70px"
        style="width: 400px; margin-left:50px;">
        <el-form-item label="品牌名">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input type="number" v-model="temp.sort" />
        </el-form-item>
        <el-form-item label="品牌图标">
          <img @click="showImageCenter" v-if="temp.ico_img" :src="temp.ico_img" class="avatar">
          <i v-else @click="showImageCenter" class="el-icon-plus avatar-uploader-icon"></i>

        </el-form-item>
        <el-form-item label="状 态">
          <el-radio-group v-model="temp.status">
            <el-radio :label="1">开启</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="small">取消</el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()" size="small">确认</el-button>
      </div>

      <ImageCenter :is_multiple=false :Innervisible.sync="dialogImageVisible" :limit=1 @change="changeImage"
        @closeImage="closeImage" />
    </el-dialog>
  </div>
</template>
<script>
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
import { fetchList, deleteBrand, createBrand, updateBrand } from '@/api/goods/goodsBrand'
import ImageCenter from '@/components/ImageCenter'

export default {
  name: 'ComplexTable',
  components: { Pagination, ImageCenter },
  directives: { waves },
  data() {
    return {
      list: null,
      listLoading: false,
      total: 0,
      listQuery: {
        size: 10,
        page: 1
      },
      temp: { name: '', ico_img: 0, status: 1 },
      dialogFormVisible: false,
      dialogImageVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑分类',
        create: '新增分类'
      },
      rules: {
        name: [{ required: true, message: '名字不能为空', trigger: 'blur' }]
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.data
        this.list.map(v => { v.status_name = v.status == 1 ? '启用' : '禁用'; })
        this.total = response.data.total
        this.listLoading = false
      })
    },
    resetTemp() {
      this.temp = { name: '', ico_img: 0, status: 1 }
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.default_select = [this.pid]
      this.temp.pid = this.pid
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.default_select = [row.pid]
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(row, index) {
      deleteBrand(row).then(response => {
        if (response.code == 200) {
          this.$notify({
            title: 'Success',
            message: 'Delete Successfully',
            type: 'success',
            duration: 2000
          })

          this.list.splice(index, 1)
        }
      })
    },
    showImageCenter() {
      this.dialogImageVisible = true;
      return false;
    },
    changeImage: function (res) {
      this.dialogImageVisible = false;
      if (res.length == 1) {
        this.temp.ico_img = res[0].url
        //console.log(this.temp)
      }
    },
    closeImage: function () {
      this.dialogImageVisible = false;
    },
    updateData() {
      this.listLoading = true
      updateBrand(this.temp).then(response => {
        if (response.code == 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
        }
        this.dialogFormVisible = false
        this.listLoading = false
        this.getList()
      })

    },
    createData() {
      this.listLoading = true
      createBrand(this.temp).then(response => {
        if (response.code == 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
        }
        this.dialogFormVisible = false
        this.listLoading = false
        this.getList()
      })
    },
  }
}

</script>
<style type="text/css">
.avatar,
.avatar-uploader-icon {
  width: 78px;
  height: 78px;
  padding: 1px;
  border: 1px solid #ccc;
  text-align: center;
  line-height: 78px;
}

.filter-container {
  background: #FFFFFF;
  padding: 0.625rem 1.125rem;
}

.filter-container {
  font-size: 12px;
}

.filter-item input[type='text'] {
  height: 32px;
  line-height: 32px;
  font-size: 12px;
}

.mring10 {
  margin-right: 10px;
}
</style>
